<template>
  <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">寻惠</h3>
      <p style="text-align:center;color:rgb(170, 170, 164)">找回密码</p>
      <div v-if="!isRetrieveValidataed">
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            type="text"
            auto-complete="off"
            placeholder="需要找回的账号"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input
            v-model="registerForm.email"
            type="email"
            auto-complete="off"
            placeholder="账号绑定的邮箱"
            @keyup.enter.native="handleRegister"
          >
            <svg-icon slot="prefix" icon-class="email" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="emailCode" v-if="captchaEnabled">
          <el-input
            v-model="registerForm.emailCode"
            auto-complete="off"
            placeholder="邮箱验证码"
            style="width: 63%"
            @keyup.enter.native="handleRegister"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="register-code">
            <el-button @click="getEmailCode">获取验证码</el-button>
          </div>
        </el-form-item>
      </div>
      <div v-else>
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            auto-complete="off"
            placeholder="修改密码"
            show-password
            @keyup.enter.native="handleRegister"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            auto-complete="off"
            placeholder="确认密码"
            show-password
            @keyup.enter.native="handleRegister"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
      </div>
      <el-form-item style="width:100%;">
        <el-button
          v-if="!isRetrieveValidataed"
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleValidate"
        >
          <span v-if="!loading">验证</span>
          <span v-else>验 证 中...</span>
        </el-button>
        <el-button
          v-else
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRetrievePassword"
        >
          <span v-if="!loading">修改密码</span>
          <span v-else>修 改 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'" @click="isRetrieveValidataed=false">返回登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>@Copy白尚杰的校园二手商品交易平台</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";
import Treeselect from "@riophae/vue-treeselect";
import { deptTreeSelect } from "@/api/system/user";
import { sendCode } from "@/api/mysystem/email";
import { Message } from "element-ui";
import { retrieveValidata,retrieveRetrievePassword } from "@/api/mysystem/retrieve";
export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      isRetrieveValidataed: false,
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        uuid: "",
        deptId: "",
        email: "",
        emailCode: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          {
            min: 2,
            max: 20,
            message: "用户账号长度必须介于 2 和 20 之间",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          {
            min: 5,
            max: 20,
            message: "用户密码长度必须介于 5 和 20 之间",
            trigger: "blur"
          }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        emailCode: [
          { required: true, trigger: "change", message: "请输入验证码" }
        ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          },
          {
            required: true,
            trigger: "blur",
            message: "请输入的邮箱地址"
          }
        ]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    this.getCode();
  },
  components: { Treeselect },
  methods: {
    //验证填写数据
    handleValidate() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          retrieveValidata(this.registerForm)
            .then(res => {
              console.log("retrieveValidata-res->", res);
              this.isRetrieveValidataed = true;
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
    //处理重置密码
    handleRetrievePassword() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          retrieveRetrievePassword(this.registerForm)
            .then(res => {
              console.log(">>>>-res->", res);
              this.$router.push("/login");
              this.isRetrieveValidataed = false;
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
    // 获取邮箱验证码
    getEmailCode() {
      if (this.registerForm.email != "") {
        sendCode(this.registerForm.email).then(res => {
          console.log("sendCode-res->", res);
        });
      } else {
        Message.error("请先输入邮箱");
      }
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm)
            .then(res => {
              const username = this.registerForm.username;
              this.$alert(
                "<font color='red'>恭喜你，您的账号 " +
                  username +
                  " 注册成功！</font>",
                "系统提示",
                {
                  dangerouslyUseHTMLString: true,
                  type: "success"
                }
              )
                .then(() => {
                  this.$router.push("/login");
                })
                .catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
